// pages/profile/profile.js
import request from '../../request/index'
//使用 async await 语法必须引入，固定写法。
import regeneratorRuntime from '../../utils/runtime'
const App = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userPlaylist:[],//用户歌单
    userInfo:{},//用户基本信息
    fourGrid: [
      { name: "今日签到", icon: "icon-qiandaolingjiang" },
      { name: "最近播放", icon: "icon-zuijinbofang" },
      { name: "我的收藏", icon: "icon-shoucang" },
      { name: "我的资料", icon: "icon-daohangzhankai-" },
    ],//小功能数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const userInfo  = App.globalData.userInfo
    this.getUserPlaylist(userInfo.userId)
    this.setData({userInfo})
  },
  //获取用户歌单
  async getUserPlaylist(uid) {
    const result = await request({url:"user/playlist",data:{uid}})
    if(result.code !== 200){
      console.log('获取用户歌单失败',result)
      return
    }
    const playlist = result.playlist
    let userPlaylist = playlist.map(item => {
      const {coverImgUrl,trackCount,name,id} = item
      return {coverImgUrl,trackCount,name,id}
    })
    //删除掉最后一个无用歌单
    userPlaylist.pop()
    this.setData({userPlaylist})

  },
  //去歌单详情页面
  goDetailPlaylist(event) {
    const {id} = event.currentTarget.dataset
    wx.navigateTo({url: '/pages/playlist-detail/playlist-detail?id=' + id})
  },
  //点击了四个小功能
  funcClick(event) {
    const {name} = event.currentTarget.dataset
    switch (name) {
      case "今日签到":
        this.signIn()
        break;
      case "最近播放":
        wx.navigateTo({url: '/pages/record-song/record-song'})
        break;
      case "我的收藏":
        wx.showToast({
          title: '暂未实现~',
          icon: 'none',
          duration: 1500,
          mask: false,
        })
        break;
      case "我的资料":
        wx.navigateTo({url: '/pages/my-info/my-info'})
        break;

      default:
        break;
    }
  },
  //今日签到功能函数
  async signIn() {
    const result = await request({url:"daily_signin"})
    if(result.code === 200){
      wx.showToast({
        title: '签到成功~',
        icon: 'none',
        duration: 1500,
        mask: false,
      })
    }else if(result.code === -2){
      wx.showToast({
        title: '今日已签~',
        icon: 'none',
        duration: 1500,
        mask: false,
      })
    }else{
      wx.showToast({
        title: '签到失败~',
        icon: 'none',
        duration: 1500,
        mask: false,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //判断是否更新过信息，更新过重新获取
    if(App.globalData.userInfo.isUpdate){
      this.setData({userInfo:App.globalData.userInfo})
      App.globalData.userInfo.isUpdate = false
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})